﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using cecbData;
using cecbClasses;
using CECB;


namespace CECB.Member
{
    /// <summary>
    /// Interaction logic for V_MemberUpdate_Window.xaml
    /// </summary>
    public partial class V_MemberUpdate_Window : Window
    {
        public V_MemberUpdate_Window()
        {
            InitializeComponent();
            autoCompleteBox_SearchMember.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getAllMembers();
            listBoxSpec.ItemsSource = new DSSEntities().Specilizations;
        }

        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }


        private void Email_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void button_Save_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {


        }

        private void textBox_ContactNo3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void textBox_ContactNo3_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Label_SourceUpdated(object sender, DataTransferEventArgs e)
        {

        }
        Members memObj = null;
        cecbClasses.Data_Adapters.Member.Func_Members getmem = new cecbClasses.Data_Adapters.Member.Func_Members();
        private void autoCompleteBox_SearchMember_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (((AutoCompleteBox)sender).SelectedItem != null)
            {
                memObj = (Members)autoCompleteBox_SearchMember.SelectedItem;

                try
                {
                    new cecbClasses.Data_Adapters.Member.Func_Members().getMemberDetails(memObj.mem_reference);
                    new cecbClasses.Data_Adapters.Member.Func_Members().getMemberLoginDetails(memObj.mem_reference);

                    List<Members> memObject = new List<Members>();
                    List<MemberLogin> memLoginObj = new List<MemberLogin>();
                    memLoginObj = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberLoginDetails(memObj.mem_reference);
                    memObject = new cecbClasses.Data_Adapters.Member.Func_Members().getMemberDetails(memObj.mem_reference);
                    textBox_ModifyAddress.Text = memObject[0].mem_address;
                    textBox_ModifyFirstName.Text = memObject[0].mem_name;
                    string memRef = memObject[0].mem_reference;

                    comboBox_ModifyDesignation.Text = memObject[0].mem_designation;
                    current_Specilixzations.ItemsSource = getmem.getMemberSpecilizations(memObj.mem_reference);
                    ModifySaveButton.IsEnabled = true;


                }
                catch (Exception)
                {


                }
            }
        }


        private void updateButton_Click(object sender, RoutedEventArgs e)
        {



            cecbClasses.Data_Adapters.Member.Func_Members memberFunction = new cecbClasses.Data_Adapters.Member.Func_Members();



            if (autoCompleteBox_SearchMember.SelectedItem == null)
                MessageBox.Show("select a member", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            else
            {

                List<Specilizations> specList = listBoxSpec.SelectedItems.Cast<Specilizations>().ToList();

                if (memberFunction.updateMemberDetails1(memObj.mem_reference, textBox_ModifyAddress.Text, textBox_ModifyFirstName.Text, comboBox_ModifyDesignation.Text, specList))
                {

                    MessageBox.Show("Member informations Successfully Updated", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                    autoCompleteBox_SearchMember.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getAllMembers();
                    current_Specilixzations.ItemsSource = getmem.getMemberSpecilizations(memObj.mem_reference);
                    clearAfterUpdate();

                }
                else
                {
                    MessageBox.Show("Selected nothing to modify", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                listBoxSpec.ItemsSource = new DSSEntities().Specilizations;
                clearAfterUpdate();


            }
        }
        public void clearAfterUpdate()
        {

            textBox_ModifyFirstName.Clear();
            textBox_ModifyAddress.Clear();
            current_Specilixzations.ItemsSource = null;
            comboBox_ModifyDesignation.SelectedIndex = -1;
            autoCompleteBox_SearchMember.Text = "";



        }

        private void textBox_Modifyusername_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox_Modifyusername_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void textBox_Modifyusername_TextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void textBox_Modifyusername_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void comboBox_Specilised_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void textBox_Designation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void WrapPanel_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }


    }
}

